﻿using System;
using System.Collections.Generic;
using System.Threading;
using Contracts.Entities;
using Contracts.Library;
using Definition.WebImport;

namespace Client.Processes
{
    public class SearchMoviesOnline
    {
        private TmdbSearch tmdbSearch;
        private Synchronizer<TmdbSearchResult> synchronizer;

        public SearchMoviesOnline()
        {
            this.tmdbSearch = new TmdbSearch();
            this.synchronizer = new Synchronizer<TmdbSearchResult>();

            this.tmdbSearch.OnResult += synchronizer.Process;
            this.synchronizer.OnCompleted += m => OnResult(m);
        }

        /// <summary>
        /// Async Search (Online Search), Take Moviename as request and Add To Queue
        /// </summary>
        public void Process(IEnumerable<MovieItem> items)
        {
            foreach (var item in items)
            {
                ThreadPool.QueueUserWorkItem(tmdbSearch.Process, item.Name);
            }
        }

        public event Action<TmdbSearchResult> OnResult;
    }
}
